<?php
require_once 'index.php';

message("Initializing Segment Database..", PT_INFORMATION);

(@include_once 'Services/JSON.php') or die(message("JSON library was not found."));

define('F_UNSPECIFIED', 	0x00000000);
define('F_PLUS', 		0x00000001);
define('F_MINUS', 		0x00000010);

define('PT_DEFAULT_CONFIG', 	'data/default.json');
$json_errors = array(
	JSON_ERROR_NONE		=>	'No error has occured.',
	JSON_ERROR_DEPTH	=>	'The maximum stack depth has been exceeded.',
	JSON_ERROR_CTRL_CHAR	=>	'Control character error, possibily incorrectly encoded',
	JSON_ERROR_SYNTAX	=>	'There was a problem with the syntax of the configuration of dataset file.'
);
$available_features = array("_WORD_BOUNDARY", "_MORPHEME_BOUNDARY");

if (file_exists(PT_DEFAULT_CONFIG)) {
	message("Reading default configuration file..", PT_DEBUG);
	$json = json_decode(file_get_contents(PT_DEFAULT_CONFIG));
	if (!$json) { 
		message("The default configuration was not available. Feature and segment definitions will have to come from the datasets used.", PT_WARNING); 
		message("JSON Error: " . $json_errors[json_last_error()], PT_FATAL_ERROR);
	} else {
		// register all the features
		foreach ($json->{'features'} as $feature) {
			add_feature($feature);
		}

		// register all the segments
		foreach ($json->{'xsampa'} as $xsampa => $unicode) {
			insert_xsampa_map($xsampa, $unicode);
		}
	}
} else {message(" The default configuration file was not found. Feature and segment definitions will have to come from the datasets used.", PT_WARNING); }

class CFeatureSet {
	private $feature_set = array();
	
	function __construct() {
		global $available_features;
		foreach ($available_features as $feature) {
			$this->feature_set[$feature] = F_UNSPECIFIED;
		}
		message("A new feature set has been created.", PT_DEBUG);
	}
	public function get_feature($feature) {
		if (array_key_exists($feature,$this->feature_set)) { 
			return $this->feature_set[$feature];
		} else { return F_UNSPECIFIED; }
	}	

	public function set_feature($feature, $value) {
		$this->feature_set[$feature] = $value;
		message("$feature set to $value.", PT_DEBUG);
	}
}

class CSegment {
	private $unicode = 63;
	private $feature_set;

	function __construct() {
		message("A new segment has been created.", PT_DEBUG);
		$this->feature_set = new CFeatureSet;
	}

	public function get_unicode() { return "&#" . $this->unicode; }
	public function set_unicode($unicode) { $this->unicode = $unicode; }
	public function set_feature($feature, $value) { 
		$this->feature_set->set_feature($feature, $value); 
		// update the unicode, if no feature set matching this new set is available, use '?'
	}
	public function get_feature($feature) { $this->feature_set->get_feature($feature); }
}

function add_feature($feature) { 
	global $available_features;
	$available_features[] = $feature;
	message("The \"$feature\" feature has been added.", PT_INFORMATION);
}

?>
